/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package javasamples.controllers;

import javasamples.Dao.IUserDao;
import javasamples.model.User;

/**
 *
 * @author MariosT
 */
public class LoginController {
    private final IUserDao dao;

    public LoginController(IUserDao dao) {
        this.dao = dao;
    }
    
    
    public boolean validateUser(String username, String password)
    {
        User user = dao.findUserByUsername(username);
        if (user==null) 
        {
            return false;
        }
        if (user.getNumOfUnsuccesfulLogins()>4) {
            return false;
        }
        if (user.getPassword().equals(password)) {
            user.setNumOfUnsuccesfulLogins(0);
        } else {
            int numOfUnsuccesfulLogins = user.getNumOfUnsuccesfulLogins();
            user.setNumOfUnsuccesfulLogins(numOfUnsuccesfulLogins+1);
            //save user
            return false;
        }
            return true;
        
    }
    
    
}
